using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace LetLord.Models
{ 
    public class ReferencePhotoRepository : IReferencePhotoRepository
    {
        LetLordContext context = new LetLordContext();

        public IQueryable<ReferencePhoto> All
        {
            get { return context.Image.OfType<ReferencePhoto>(); }
        }

        public IQueryable<ReferencePhoto> AllIncluding(params Expression<Func<ReferencePhoto, object>>[] includeProperties)
        {
            IQueryable<ReferencePhoto> query = context.Image.OfType<ReferencePhoto>(); ;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public ReferencePhoto Find(int id)
        {
            return context.Image.OfType<ReferencePhoto>().SingleOrDefault(r => r.ImageId == id);
        }

        public void InsertOrUpdate(ReferencePhoto referencephoto)
        {
            if (referencephoto.ImageId == default(int)) {
                // New entity
                context.Set<ReferencePhoto>().Add(referencephoto);
            } else {
                // Existing entity
                context.Entry(referencephoto).State = EntityState.Modified;
            }
        }



        public void Delete(int id)
        {
            var referencephoto = context.Image.OfType<ReferencePhoto>().SingleOrDefault(r => r.ImageId == id);
            context.Set<ReferencePhoto>().Remove(referencephoto);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IReferencePhotoRepository : IDisposable
    {
        IQueryable<ReferencePhoto> All { get; }
        IQueryable<ReferencePhoto> AllIncluding(params Expression<Func<ReferencePhoto, object>>[] includeProperties);
        ReferencePhoto Find(int id);
        void InsertOrUpdate(ReferencePhoto referencephoto);
        void Delete(int id);
        void Save();
    }
}